package com.ruoyi.sl.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sl.domain.SlDbinfo;
import com.ruoyi.sl.mapper.SlDbinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sl.mapper.SlEventMapper;
import com.ruoyi.sl.domain.SlEvent;
import com.ruoyi.sl.service.ISlEventService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 情况汇报Service业务层处理
 * 
 * @author admin
 * @date 2024-06-04
 */
@Service
public class SlEventServiceImpl implements ISlEventService 
{
    @Autowired
    private SlEventMapper slEventMapper;

    @Autowired
    private SlDbinfoMapper slDbinfoMapper;

    /**
     * 查询情况汇报
     * 
     * @param eventId 情况汇报主键
     * @return 情况汇报
     */
    @Override
    public SlEvent selectSlEventByEventId(Long eventId)
    {
        return slEventMapper.selectSlEventByEventId(eventId);
    }

    /**
     * 查询情况汇报列表
     * 
     * @param slEvent 情况汇报
     * @return 情况汇报
     */
    @Override
    public List<SlEvent> selectSlEventList(SlEvent slEvent)
    {
        return slEventMapper.selectSlEventList(slEvent);
    }

    /**
     * 新增情况汇报
     * 
     * @param slEvent 情况汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSlEvent(SlEvent slEvent)
    {
        slEvent.setCreateTime(DateUtils.getNowDate());
        int rows = slEventMapper.insertSlEvent(slEvent);
        //0草稿直接存，1提交需要创建待办到审核人
        if(slEvent.getIsDone().equals("1")){
            slEvent.setSubmitTime(DateUtils.getNowDate());
            SlDbinfo slDbinfo  = createDbinfoByEvent(slEvent);
            slDbinfoMapper.insertSlDbinfo(slDbinfo);
        }

        return rows;
    }

    /**
     * 修改情况汇报
     * 
     * @param slEvent 情况汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlEvent(SlEvent slEvent)
    {

        slEvent.setUpdateTime(DateUtils.getNowDate());
        int rows = slEventMapper.updateSlEvent(slEvent);
        //0草稿直接存，1提交需要创建待办到审核人
        if(slEvent.getIsDone().equals("1")){
            slEvent.setSubmitTime(DateUtils.getNowDate());
            SlDbinfo slDbinfo  = createDbinfoByEvent(slEvent);
            slDbinfoMapper.insertSlDbinfo(slDbinfo);
        }
        return rows;
    }

    /**
     * 创建待办信息
     *
     * @param slEvent 汇报信息
     * @return 结果
     */
    public SlDbinfo createDbinfoByEvent(SlEvent slEvent)
    {

        SlDbinfo slDbinfo = new SlDbinfo();
        slDbinfo.setDbLevel(slEvent.getEventLevel());   //级别
        slDbinfo.setDbSubject(slEvent.getEventSubject());    //标题
        slDbinfo.setDbType(slEvent.getEventType());   //类型
        slDbinfo.setIsDb("0");  //待办
        slDbinfo.setReceptionTime(DateUtils.getNowDate()); //接收时间
        slDbinfo.setPresenterNick(SecurityUtils.getLoginUser().getUser().getNickName());    //提交人名称
        slDbinfo.setPresenterUser(SecurityUtils.getLoginUser().getUser().getUserName());    //提交人ID
        slDbinfo.setTransactorsNick(slEvent.getEventNick());  //处理人名称
        slDbinfo.setTransactorsUser(slEvent.getEventUser());  //处理人ID
        slDbinfo.setIsDone("1");   //状态
        slDbinfo.setSource("hb");   //汇报
        slDbinfo.setSourceId(slEvent.getEventId()); //来源ID

        return slDbinfo;
    }


    /**
     * 批量删除情况汇报
     * 
     * @param eventIds 需要删除的情况汇报主键
     * @return 结果
     */
    @Override
    public int deleteSlEventByEventIds(Long[] eventIds)
    {
        return slEventMapper.deleteSlEventByEventIds(eventIds);
    }

    /**
     * 删除情况汇报信息
     * 
     * @param eventId 情况汇报主键
     * @return 结果
     */
    @Override
    public int deleteSlEventByEventId(Long eventId)
    {
        return slEventMapper.deleteSlEventByEventId(eventId);
    }



}
